#include "../include/HTTP.hpp"
// HTTP::HTTP(std::shared_ptr<HttpRequest>& head){
//     ReqHead=head;
// }
HTTP::HTTP(int fd,int _epfd,struct epoll_event* _event,std::shared_ptr<HttpRequest>& head):client_fd(fd),epfd(_epfd),event(_event){
    ReqHead=head;
}
int HTTP::reuqest(){
    if(ReqHead->getMethod()=="GET"){  // 如果为get请求，则根据当前请求头中的url中从 urls 获取 处理函数。
        std::shared_ptr<Url> u=urls.getUrl(ReqHead->getUrl(),ReqHead->getUrlParams());
        std::shared_ptr<Response> res=u->fun(this);
        if(res->getSend()==0){
            std::string resValue=res->returnResponse();
            write(client_fd,resValue.c_str(),resValue.size());
        }
        
        if(res->getClose()==1){
            epoll_ctl(epfd,EPOLL_CTL_DEL,client_fd,event);
            close(client_fd);
        }
        
    }
    return 0;
}


std::shared_ptr<HttpRequest>& HTTP::get_ReqHead(){
    return ReqHead;
}

// int get_fd(){
//     return 
// }